package com.cn.lyl.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.lyl.model.UserModel;
import com.cn.lyl.service.UserService;
import com.cn.lyl.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/back/user/add")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //        1.得到前端传递过来的数据
//        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String mobile = req.getParameter("mobile");
        String gender = req.getParameter("gender");
        String hobby = req.getParameter("hobby");
        String birthday = req.getParameter("birthday");

//          2.封装对象
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(password);
        userModel.setMobile(mobile);
        userModel.setGender(gender);
        userModel.setHobby(hobby);
        userModel.setBirthday(birthday);
//        调用service
        UserService userService = new UserServiceImpl();
        //        -返回值
        String result = userService.addUser(userModel);
//       反馈给前端--返回值
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);

    }
}
